var Blog = require('../model/blog');
var Comment = require('../model/comment');

/**
 * 查询
 */
exports.fetch = function(req, res) {
    Blog
        .find()
        .populate('author', 'nickName')
        .exec(function(err, blogs) {
            if (err) {
                console.log(err);
            } else {
                res.render('pages/blog', {
                    blogs: blogs
                })
            }
        });
}

exports.findOne = function(req, res) {
    var id = req.params.id;
    Blog.findOne({
            _id: id
        })
        .populate('author', 'nickName')
        .exec(function(err, blog) {
            Comment
                .find({
                    blog: id
                })
                .populate('from reply.from reply.to', 'nickName face')
                .exec(function(err, comments) {
                    console.log(comments)
                    res.render('pages/single', {
                        blog: blog,
                        comments: comments
                    })

                });
        });
}
